﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.GERP.GIMS.MODEL;
using System.Data;
using KPIS.DBM;
using System.Configuration;

namespace KPIS.GERP.GIMS.DAL
{
    public class D_JobSiteSeq
    {
        DBManager dbManager = new DBManager(DataProvider.MySQL, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();
        public IList<M_JobSiteSeq> GetList()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_JobSiteSeq> infoList = new List<M_JobSiteSeq>();
            try
            {
                dbManager.Open();
                strSQL =    " SELECT DISTINCT job_site_seq,job_site_ref,job_site_name "
                          + " FROM hrm_mp_job_sites "
                          + " WHERE record_status <> 'D' ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_JobSiteSeq info = new M_JobSiteSeq();
                        info.job_site_seq = Convert.ToInt32(drc[i]["job_site_seq"]);
                        info.job_site_ref = drc[i]["job_site_ref"].ToString();
                        info.job_site_name = drc[i]["job_site_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_JobSiteSeq GetListFillBySeq(Nullable<Int32> job_site_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_JobSiteSeq info = new M_JobSiteSeq();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@job_site_seq", job_site_seq);

                strSQL =    " SELECT DISTINCT job_site_seq,job_site_ref,job_site_name "
                          + " FROM hrm_mp_job_sites "
                          + " WHERE record_status <> 'D' "
                          + " and job_site_seq = @job_site_seq ";      

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.job_site_seq = Convert.ToInt32(drc[0]["job_site_seq"]);
                    info.job_site_ref = drc[0]["job_site_ref"].ToString();
                    info.job_site_name = drc[0]["job_site_name"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }
        public IList<M_JobSiteSeq> GetListFilterByName(string job_site_name)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_JobSiteSeq> infoList = new List<M_JobSiteSeq>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT DISTINCT job_site_seq,job_site_ref,job_site_name "
                          + " FROM hrm_mp_job_sites "
                          + " WHERE record_status <> 'D' and job_site_name LIKE '%' '" + job_site_name + "'  '%'";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_JobSiteSeq info = new M_JobSiteSeq();
                        info.job_site_seq = Convert.ToInt32(drc[i]["job_site_seq"]);
                        info.job_site_ref = drc[i]["job_site_ref"].ToString();
                        info.job_site_name = drc[i]["job_site_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

    }
}
